﻿//-----------------------------------------------------------------------
// <copyright file="NavigationService.cs" company="deixei.com">
//     Copyright (c) deixei.com. All rights reserved.
// </copyright>
// <author>Marcio Parente</author>
//---------------------------------------------------------------------
namespace Deixei.Domain.Service
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using Deixei;
    using Deixei.Domain.Repository;
    using Deixei.Infrastructure;
    using Microsoft.Practices.Unity;

    public class NavigationService : INavigationService
    {
        public NavigationService()
        {
            Initialize(null, null, null, null);
        }

        public NavigationService(IWebAppPortfolioRepository webAppPortRepo, INavigationAreaRepository areaRepo, INavigationControllerRepository controllerRepo, IUnitOfWork unitOfWork)
        {
            Initialize(webAppPortRepo, areaRepo, controllerRepo, unitOfWork);
        }

        [Dependency]
        public IWebAppPortfolioRepository WebAppPortfolioRepository { get; set; }

        [Dependency]
        public INavigationControllerRepository NavigationControllerRepository { get; set; }

        [Dependency]
        public INavigationAreaRepository NavigationAreaRepository { get; set; }

        [Dependency]
        public IUnitOfWork UnitOfWork { get; set; }

        // Execution of Services

        // GetAreas
        public IQueryable<NavigationArea> GetNavigationAreas(int ApplicationId)
        {
            IQueryable<NavigationArea> data = NavigationAreaRepository.Find(row => row.WebAppPortfolio.Id == ApplicationId).OrderBy(o => o.Area);
            return data;
        }
        
        // GetControllerByArea
        public IQueryable<NavigationController> GetControllerByArea(int areaId)
        {
            NavigationArea area = NavigationAreaRepository.Single(item => item.Id == areaId, include => include.Controllers);
            return area.Controllers.AsQueryable().OrderBy(item => item.Controller);
        }

        // AddArea
        public NavigationArea AddNavigationArea(NavigationArea newObject)
        {
            NavigationAreaRepository.Add(newObject);
            NavigationAreaRepository.UnitOfWork.Save();
            return newObject;
        }

        // EditArea
        public NavigationArea EditNavigationArea(NavigationArea updatedObject)
        {
            NavigationArea item = NavigationAreaRepository.Single(row => row.Id == updatedObject.Id);

            item.GroupName = updatedObject.GroupName;
            item.Area = updatedObject.Area;
            item.Description = updatedObject.Description;
            item.Url = updatedObject.Url;

            NavigationAreaRepository.Update(item);
            NavigationAreaRepository.UnitOfWork.Save();
            return updatedObject;
        }

        // AddController
        public NavigationController AddNavigationController(NavigationArea area, NavigationController controller)
        {
            controller.NavigationArea = area;
            NavigationControllerRepository.Add(controller);
            NavigationControllerRepository.UnitOfWork.Save();
            return controller;
        }

        // RemoveArea
        public NavigationArea DeleteNavigationArea(int id)
        {
            NavigationArea itemToDelete = NavigationAreaRepository.Single(m => m.Id == id);
            NavigationAreaRepository.Delete(itemToDelete);
            NavigationControllerRepository.UnitOfWork.Save();
            return itemToDelete;
        }

        // RemoveController


        // WebAppPortfolio Service Start
        // WebAppPortfolioRepository
        // Get(), Add(WebAppPortfolio), Edit(WebAppPortfolio), Delete(Id)
        public IQueryable<WebAppPortfolio> GetWebAppPortfolio() 
        {
            IQueryable<WebAppPortfolio> data = WebAppPortfolioRepository.GetAll().OrderBy(item => item.Id);
            return data;
        }

        public WebAppPortfolio AddWebAppPortfolio(WebAppPortfolio newObject) 
        {
            WebAppPortfolioRepository.Add(newObject);
            WebAppPortfolioRepository.UnitOfWork.Save();
            return newObject;
        }
        public WebAppPortfolio EditWebAppPortfolio(WebAppPortfolio updatedObject) 
        {
            WebAppPortfolioRepository.Update(updatedObject);
            WebAppPortfolioRepository.UnitOfWork.Save();
            return updatedObject;
        }
        public WebAppPortfolio DeleteWebAppPortfolio(int id) 
        {
            WebAppPortfolio itemToDelete = WebAppPortfolioRepository.Single(m => m.Id == id);
            WebAppPortfolioRepository.Delete(itemToDelete);
            WebAppPortfolioRepository.UnitOfWork.Save();
            return itemToDelete;
        }
        // WebAppPortfolio Service End

        private void Initialize(IWebAppPortfolioRepository webAppPortRepo, INavigationAreaRepository areaRepo, INavigationControllerRepository controllerRepo, IUnitOfWork unitOfWork)
        {
            UnitOfWork = ( unitOfWork ?? DeixeiContainer.Container.Resolve<UnitOfWork>() );

            NavigationAreaRepository = new NavigationAreaRepository(UnitOfWork);

            NavigationAreaRepository = ( areaRepo ?? DeixeiContainer.Container.Resolve<NavigationAreaRepository>() );
            NavigationControllerRepository = ( controllerRepo ?? DeixeiContainer.Container.Resolve<NavigationControllerRepository>() );
            WebAppPortfolioRepository = ( webAppPortRepo ?? DeixeiContainer.Container.Resolve<WebAppPortfolioRepository>() );
        }
    }

    public interface INavigationService
    {
        IQueryable<NavigationArea> GetNavigationAreas(int ApplicationId);
        IQueryable<NavigationController> GetControllerByArea(int areaId);

        NavigationArea AddNavigationArea(NavigationArea newObject);
        NavigationController AddNavigationController(NavigationArea area, NavigationController controller);
        NavigationArea EditNavigationArea(NavigationArea updatedObject);

        NavigationArea DeleteNavigationArea(int id);

        // WebAppPortfolioRepository
        // Get(), Add(WebAppPortfolio), Edit(WebAppPortfolio), Delete(Id)
        IQueryable<WebAppPortfolio> GetWebAppPortfolio();
        WebAppPortfolio AddWebAppPortfolio(WebAppPortfolio newObject);
        WebAppPortfolio EditWebAppPortfolio(WebAppPortfolio updatedObject);
        WebAppPortfolio DeleteWebAppPortfolio(int id);

    }
}
